package cn.net.zidian.mvvm.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import cn.net.zidian.databinding.ActivityAstroBinding;
import cn.net.zidian.mvvm.adapter.AstroAdapter;
import cn.net.zidian.mvvm.vm.AstroVM;

public class AstroActivity extends BaseActivity<AstroVM> {

    private ActivityAstroBinding viewBinding;

    private AstroAdapter adapter = new AstroAdapter();

    @Override
    protected AstroVM viewModel() {
        return new AstroVM();
    }

    @Override
    protected void initView() {
        initObserver();
        viewBinding = ActivityAstroBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        viewBinding.titleBar.getLeftView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        viewBinding.rvList.setLayoutManager(new LinearLayoutManager(this));
        viewBinding.rvList.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                Intent i = new Intent(AstroActivity.this, AstroDetailsActivity.class);
                i.putExtra("id", adapter.getItem(position).getAstroid());
                i.putExtra("name", adapter.getItem(position).getAstroname());
                startActivity(i);
            }
        });

        vm.getList();
    }

    public void initObserver() {
        vm.eventList.observe(this, list -> adapter.setNewData(list));
    }

}
